// components/diy/goods/index.js
import { getSeckillIndexTime, getSeckillList,getBargainList,getCombinationList } from '../../../api/activity.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    diyPramas:{
      type:Object
    },
    diyStyle:{
      type:Object
    },
    diyData:{
      type:Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    swiperCurrent: 0,
    slider:[],
    swiperBtnHeight:0, //图组高度
    lineSize:0, //行数
    pageSize:0,  //页数
    goodsdata:[],
    stopTime:'',
    combinationList:[], //拼团
    skillList:[], //秒杀
    bargainList:[] //砍价
  },

  /**
   * 组件的方法列表
   */
  methods: {
     //轮播图的切换事件
    swiperChange: function(e){
      //只要把切换后当前的index传给<swiper>组件的current属性即可
      this.setData({
        swiperCurrent: e.detail.current
      })
    },

    //点击指示点切换
    chuangEvent: function(e){
      this.setData({
        swiperCurrent: e.currentTarget.id
      })
    },
  },
  ready: function(){
    //goodsdata == 0 手动选择商品
    //goodsdata == 2 拼团商品
    //goodsdata == 3 秒杀商品
    //goodsdata == 4 砍价商品
    var goodsdata = this.properties.diyPramas.goodsdata
    this.setData({
      goodsdata:goodsdata
    })
    if(goodsdata == 2){
      //请求拼团数据
      var data = { page: 1, limit: 10};
      var that = this
      getCombinationList(data).then(function (res) {
        that.setData({
          combinationList:res.data
        })
        var goodsscroll = that.properties.diyPramas.goodsscroll  //显示类型 0 普通模式  1 单行滑动  un 列表
        if(goodsscroll == '1'){
          var num = 0; //总共图片数
          if(goodsdata == '2'){
            num = that.data.combinationList.length;
          }else{
            num = that.data.diyData.length; 
          }
          var liststyle = that.properties.diyStyle.liststyle;
          var rownum = 0;//每行显示个数
          if(liststyle == 'block one'){
            rownum = 1
          }else if(liststyle == 'block'){
            rownum = 2
          }else if(liststyle == 'block three'){
            rownum = 3
          }
          var pageSize =0;//总共页数
          if(num%rownum == 0){
            pageSize = parseInt(num/rownum)
          }else{
            pageSize = parseInt(num/rownum) + 1
          }
          that.setData({
              pageSize:pageSize,
          }) 
          var showproductprice = that.properties.diyPramas.showproductprice
          var query = wx.createSelectorQuery().in(that)
          query.select('.goodsSwiper').boundingClientRect((rect) => {
            var width = rect.width;
            var height = ''
            if(showproductprice == '1'){
              height = (width -18)/rownum + 120
            }else{
              height = (width -18)/rownum + 65
            }
            
            that.setData({
              swiperBtnHeight: height
            })
          }).exec()
        }
      })
      
    }else if(goodsdata == 3){
      //请求秒杀数据
      let that = this; 
      getSeckillIndexTime().then(res=>{
        var data = res.data;
        if(data.seckillTime.length > 0){
            var skillId = res.data.seckillTime[res.data.seckillTimeIndex].id;
            var stopTime = res.data.seckillTime[res.data.seckillTimeIndex].stop;
            var skillData = { page: 1, limit: 4}
            that.setData({
              stopTime:stopTime
            })
            getSeckillList(skillId, skillData).then(skillres=>{
                that.setData({
                  skillList:skillres.data
                })
                var goodsscroll = that.properties.diyPramas.goodsscroll  //显示类型 0 普通模式  1 单行滑动  un 列表
                if(goodsscroll == '1'){
                  var num = 0; //总共图片数
                  if(goodsdata == '3'){
                    num = that.data.skillList.length;
                  }else{
                    num = that.data.diyData.length; 
                  }
                  var liststyle = that.properties.diyStyle.liststyle;
                  var rownum = 0;//每行显示个数
                  if(liststyle == 'block one'){
                    rownum = 1
                  }else if(liststyle == 'block'){
                    rownum = 2
                  }else if(liststyle == 'block three'){
                    rownum = 3
                  }
                  var pageSize =0;//总共页数
                  if(num%rownum == 0){
                    pageSize = parseInt(num/rownum)
                  }else{
                    pageSize = parseInt(num/rownum) + 1
                  }
                  that.setData({
                      pageSize:pageSize,
                  }) 
                  var showproductprice = that.properties.diyPramas.showproductprice
                  var query = wx.createSelectorQuery().in(that)
                  query.select('.goodsSwiper').boundingClientRect((rect) => {
                    var width = rect.width;
                    var height = ''
                    if(showproductprice == '1'){
                      height = (width -18)/rownum + 120
                    }else{
                      height = (width -18)/rownum + 65
                    }
                    
                    that.setData({
                      swiperBtnHeight: height
                    })
                  }).exec()
                }
            })
        } 
      });
    }else if(goodsdata == 4){
      //请求砍价数据
      let that = this; 
      getBargainList({page:1,limit:10}).then(function (res) {
          that.setData({
            bargainList:res.data
          })
          console.log(that.data.bargainList)
          var goodsscroll = that.properties.diyPramas.goodsscroll  //显示类型 0 普通模式  1 单行滑动  un 列表
          if(goodsscroll == '1'){
            var num = 0; //总共图片数
            if(goodsdata == '4'){
              num = that.data.bargainList.length;
            }else{
              num = that.data.diyData.length; 
            }
            var liststyle = that.properties.diyStyle.liststyle;
            var rownum = 0;//每行显示个数
            if(liststyle == 'block one'){
              rownum = 1
            }else if(liststyle == 'block'){
              rownum = 2
            }else if(liststyle == 'block three'){
              rownum = 3
            }
            var pageSize =0;//总共页数
            if(num%rownum == 0){
              pageSize = parseInt(num/rownum)
            }else{
              pageSize = parseInt(num/rownum) + 1
            }
            that.setData({
                pageSize:pageSize,
            }) 
            var showproductprice = that.properties.diyPramas.showproductprice
            var query = wx.createSelectorQuery().in(that)
            query.select('.goodsSwiper').boundingClientRect((rect) => {
              var width = rect.width;
              var height = ''
              if(showproductprice == '1'){
                height = (width -18)/rownum + 120
              }else{
                height = (width -18)/rownum + 65
              }
              that.setData({
                swiperBtnHeight: height
              })
            }).exec()
          }
       })
    }else{
      var goodsscroll = this.properties.diyPramas.goodsscroll  //显示类型 0 普通模式  1 单行滑动  un 列表
      if(goodsscroll == '1'){
        var num = this.data.diyData.length;  //总共图片数
        var liststyle = this.properties.diyStyle.liststyle;
        var rownum = 0;//每行显示个数
        if(liststyle == 'block one'){
          rownum = 1
        }else if(liststyle == 'block'){
          rownum = 2
        }else if(liststyle == 'block three'){
          rownum = 3
        }
        var pageSize =0;//总共页数
        if(num%rownum == 0){
          pageSize = parseInt(num/rownum)
        }else{
          pageSize = parseInt(num/rownum) + 1
        }

        this.setData({
            pageSize:pageSize,
        }) 
        var that = this
        var showproductprice = this.properties.diyPramas.showproductprice
        var query = wx.createSelectorQuery().in(this)
        query.select('.goodsSwiper').boundingClientRect((rect) => {
          var width = rect.width;
          var height = ''
          if(showproductprice == '1'){
            height = (width -18)/rownum + 110
          }else{
            height = (width -18)/rownum + 55
          }
          
          this.setData({
            swiperBtnHeight: height
          })
        }).exec()
      }
    }

    
    
  }
})
